package com.cy.store.config;

import com.cy.store.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @PROJECT_NAME: store
 * @DESCRIPTION:
 * @USER: 28416
 * @DATE: 2022/11/3 14:19
 *
 * 借助这个类实现拦截器实现
 */
@Configuration  //加载到容器中
public class LoginInterceptorConfig   implements WebMvcConfigurer {
    /**
     * 整体用来配置拦截器
     * @param registry
     */

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        HandlerInterceptor loginInterceptor = new LoginInterceptor();
        //配置白名单。存放在一个list集合中
        List<String> patterns  = new ArrayList<String>();
        patterns.add("/bootstrap3/**");
        patterns.add("/css/**");
        patterns.add("/images/**");
        patterns.add("/js/**");
        patterns.add("/web/register.html");
        patterns.add("/web/login.html");
        patterns.add("/web/index.html");
        patterns.add("/web/product.html");
        patterns.add("/users/reg");
        patterns.add("/users/login");
        patterns.add("/districts/**");
        patterns.add("/products/**");
        patterns.add("/carts/**");
        patterns.add("/users/**");
        //拦截器的注册
       registry.addInterceptor(loginInterceptor).addPathPatterns("/**")//要拦截的url是什么
                                .excludePathPatterns(patterns);//白名单

    }
}
